class Solution1 {
    public int numDistinct(String s, String t) {
        int n = s.length(), m = t.length();
        int[][]dp = new int[n+1][m+1];
        for(int i = 0; i<=n; i++) {
            dp[i][0] = 1;
        }
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                if(s.charAt(i) == t.charAt(j)) {
                    dp[i][j] = dp[i-1][j-1];
                }
                dp[i][j] += dp[i-1][j];
            }
        }

        return dp[n][m];
    }
}


class Solution {
    public boolean isMatch(String s, String p) {
        int n = s.length(), m = p.length();
        boolean[][] dp = new boolean[n+1][m+1];
        s = " " + s;
        p = " " + p;

        for(int i = 1; i<= m; i++) {
            if(p.charAt(i) == '*') {
                dp[i][0] = true;
            }else {
                break;
            }
        }

        for(int i = 1; i<= n; i++) {  // s
            for(int j = 1; j<=m ; j++) {  // p
                char ch = p.charAt(j);
                if(ch >= 'a' && ch <= 'z') {
                    if(ch == s.charAt(i) && dp[i-1][j-1]) {
                        dp[i][j] = true;
                    }
                }else if(ch == '?' && dp[i-1][j-1]) {
                    dp[i][j] = true;
                }else if(ch == '*') {
                    for(int k = i; k>=0; k--) {
                        if(dp[k][j-1]) {
                            dp[i][j] = true;
                            break;
                        }
                    }
                }
            }
        }

        return dp[n][m];
    }
}
public class Test {

}
